package com.langyue.recordmanage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langyue.recordmanage.enums.UserTypeEnum;
import com.langyue.recordmanage.mapper.UserMapper;
import com.langyue.recordmanage.model.query.UserPageQuery;
import com.langyue.recordmanage.model.query.UserQuery;
import com.langyue.recordmanage.pojo.Student;
import com.langyue.recordmanage.pojo.Teacher;
import com.langyue.recordmanage.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * @Description:
 * @Author: wanglang
 * @CreateTime: 2022/4/1
 * @Company:
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    public User queryById(Long id){
        return userMapper.selectById(id);
    }

    /**
     * 添加用户
     * @param userQuery
     */
    public void add(UserQuery userQuery){
        if (UserTypeEnum.ADMIN.getType() == userQuery.getUserType()){
            // 如果是管理员用户
            User user = new User(null,UserTypeEnum.ADMIN.getType(),userQuery.getUserName(),null,userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.insert(user);
        }else if(UserTypeEnum.TEACHER.getType() == userQuery.getUserType()){
            // 如果是教师
            Teacher teacher = new Teacher(null,userQuery.getUserName(),userQuery.getNumber(),userQuery.getSex(),userQuery.getInstitute(),userQuery.getTitle(),System.currentTimeMillis(),System.currentTimeMillis());
            teacherService.add(teacher);
            User user = new User(null,UserTypeEnum.TEACHER.getType(),userQuery.getUserName(),teacher.getId(),userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.insert(user);
        }else if (UserTypeEnum.STUDENT.getType() == userQuery.getUserType()){
            // 如果是学生
            Student student = new Student(null,userQuery.getUserName(),userQuery.getNumber(),userQuery.getSex(),userQuery.getInstitute(),userQuery.getSClass(),System.currentTimeMillis(),System.currentTimeMillis());
            studentService.add(student);
            User user = new User(null,UserTypeEnum.STUDENT.getType(),userQuery.getUserName(),student.getId(),userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.insert(user);
        }
    }

    /**
     * 更新用户
     * @param userQuery
     */
    public void update(UserQuery userQuery){
        User oldUser = userMapper.selectById(userQuery.getId());
        if (UserTypeEnum.ADMIN.getType() == userQuery.getUserType()){
            // 如果是管理员用户
            User user = new User(userQuery.getId(),UserTypeEnum.ADMIN.getType(),userQuery.getUserName(),null,userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.updateById(user);
        }else if(UserTypeEnum.TEACHER.getType() == userQuery.getUserType()){
            // 如果是教师
            Teacher teacher = new Teacher(oldUser.getUserId(),userQuery.getUserName(),userQuery.getNumber(),userQuery.getSex(),userQuery.getInstitute(),userQuery.getTitle(),System.currentTimeMillis(),System.currentTimeMillis());
            teacherService.updateById(teacher);
            User user = new User(userQuery.getId(),UserTypeEnum.TEACHER.getType(),userQuery.getUserName(),teacher.getId(),userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.updateById(user);
        }else if (UserTypeEnum.STUDENT.getType() == userQuery.getUserType()){
            // 如果是学生
            Student student = new Student(oldUser.getUserId(),userQuery.getUserName(),userQuery.getNumber(),userQuery.getSex(),userQuery.getInstitute(),userQuery.getSClass(),System.currentTimeMillis(),System.currentTimeMillis());
            studentService.updateById(student);
            User user = new User(userQuery.getId(),UserTypeEnum.TEACHER.getType(),userQuery.getUserName(),student.getId(),userQuery.getNumber(),userQuery.getPassword(),System.currentTimeMillis(),System.currentTimeMillis(),userQuery.getPhone(),userQuery.getEmail());
            userMapper.updateById(user);
        }
    }

    /**
     * 通过id删除用户
     * @param id
     */
    public void deleteById(Long id){
        User user = queryById(id);
        if (UserTypeEnum.TEACHER.getType() == user.getUserType()){
            // 如果是教师
            teacherService.deleteById(user.getUserId());
        }else if(UserTypeEnum.STUDENT.getType() == user.getUserType()){
            // 如果是学生
            studentService.deleteById(user.getUserId());
        }
        userMapper.deleteById(id);
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    public IPage<User> userIPage(UserPageQuery query){
        IPage<User> userPage = new Page<>(query.getPageIndex(), query.getPageSize());
        QueryWrapper<User> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(query.getUserName())){
            wrapper.eq("user_name",query.getUserName());
        }
        if (StringUtils.isNotEmpty(query.getNumber())){
            wrapper.eq("number",query.getNumber());
        }
        if (StringUtils.isNotEmpty(query.getPhone())){
            wrapper.eq("phone",query.getPhone());
        }
        if (null != query.getUserType()){
            wrapper.eq("user_type",query.getUserType());
        }
        return userMapper.selectPage(userPage, wrapper);
    }

    /**
     * 暂时 只做 管理员识别码/学号/教师号 登录
     * @param number 管理员识别码/学号/教师号
     * @param password 密码
     * @return
     */
    public boolean login(String number, String password){
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("number",number);
        User user = userMapper.selectOne(wrapper);
        if (password.equals(user.getPassword())){
            return true;
        }
        return false;
    }
}

































